import request from '@/common/utils/request'

const approval = {
  state: {
    currentStep: 0,
    basicInfo: {},
    formDesign: [],
    processDesign: [],
    moreSetting: {},
    detail: {}, // 接口返回的detail信息
    submitLoading: false, // 提交按钮loading
    villageList:[],
  },
  mutations: {
    SET_CURRENT_STEP: (state, step) => {
      state.currentStep = step;
    },
    SET_BASIC_INFO: (state, info) => {
      state.basicInfo = { ...state.basicInfo, ...info };
    },
    SET_FORM_DESIGN: (state, data) => {
      state.formDesign = data;
    },
    SET_PROCESS_DESIGN: (state, data) => {
      state.processDesign = data;
    },
    SET_MORE_SETTING: (state, info) => {
      state.moreSetting = { ...state.moreSetting, ...info };
    },
    SET_DETAIL: (state, detail) => {
      state.detail = detail;
    },
    SET_SUBMIT_LOADING: (state) => {
      state.submitLoading = !state.submitLoading;
    },
    Set_Village_List: (state,list) => {
      state.villageList =  list ;
    },
  },
  actions: {
    SetCurrentStep: ({ commit }, step) => {
      commit("SET_CURRENT_STEP", step);
    },
    SetBasicInfo: ({ commit }, info) => {
      return new Promise((resolve) => {
        commit("SET_BASIC_INFO", info);
        resolve();
      });
    },
    SetFormDesign: ({ commit }, data) => {
      return new Promise((resolve) => {
        commit("SET_FORM_DESIGN", data);
        resolve();
      });
    },
    SetProcessDesign: ({ commit }, data) => {
      return new Promise((resolve) => {
        commit("SET_PROCESS_DESIGN", data);
        resolve();
      });
    },
    SetMoreSetting: ({ commit }, data) => {
      return new Promise((resolve) => {
        commit("SET_MORE_SETTING", data);
        resolve();
      });
    },
    SetDetail: ({ commit }, detail) => {
      commit("SET_DETAIL", detail);
    },
    SetSubmitLoading: ({ commit }) => {
      return new Promise((resolve) => {
        commit("SET_SUBMIT_LOADING");
        resolve();
      });
    },
    SetVillageList:({commit})=> {
      request
        .get("/org", "/build/villageAndBuildList", {checked: 1})
        .then((res) => {
          const vList = res.villageList.map((item) => {
            return {
              label: item.name,
              value: item.id,
              // children: item.buildList.map((childItem) => {
              //   return {
              //     label: childItem.build_name,
              //     value: childItem.id,
              //   };
              // }),
            };
          });
          commit("Set_Village_List", vList);
        });
    },
  },
};

export default approval;
